<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Redux三分钟入门 | 在路上</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Redux三分钟入门什么是Redux？对于前端页面，从数据层面来说，无非就是一系列的状态组合。在传统的前端开发中，尤其是jQuery横行的年代，状态管理相关的理念一直没有什么起色。自Flux横空出世以来，各种解决方案层出不穷，百花齐放，Redux也是在这种环境下诞生的。如何描述Redux呢？简而言之，它就是一个状态容器，里面存储了整个应用的所有状态。  Redux的核心思想就是要提供_可预测_的状">
<meta name="keywords" content="Redux">
<meta property="og:type" content="article">
<meta property="og:title" content="Redux三分钟入门">
<meta property="og:url" content="http://yoursite.com/2016/02/21/redux/index.html">
<meta property="og:site_name" content="在路上">
<meta property="og:description" content="Redux三分钟入门什么是Redux？对于前端页面，从数据层面来说，无非就是一系列的状态组合。在传统的前端开发中，尤其是jQuery横行的年代，状态管理相关的理念一直没有什么起色。自Flux横空出世以来，各种解决方案层出不穷，百花齐放，Redux也是在这种环境下诞生的。如何描述Redux呢？简而言之，它就是一个状态容器，里面存储了整个应用的所有状态。  Redux的核心思想就是要提供_可预测_的状">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-03-26T11:35:06.696Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Redux三分钟入门">
<meta name="twitter:description" content="Redux三分钟入门什么是Redux？对于前端页面，从数据层面来说，无非就是一系列的状态组合。在传统的前端开发中，尤其是jQuery横行的年代，状态管理相关的理念一直没有什么起色。自Flux横空出世以来，各种解决方案层出不穷，百花齐放，Redux也是在这种环境下诞生的。如何描述Redux呢？简而言之，它就是一个状态容器，里面存储了整个应用的所有状态。  Redux的核心思想就是要提供_可预测_的状">
  
    <link rel="alternate" href="/atom.xml" title="在路上" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">在路上</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">一位崇尚价值投资的 Web 开发者</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-redux" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/02/21/redux/" class="article-date">
  <time datetime="2016-02-20T16:00:00.000Z" itemprop="datePublished">2016-02-21</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Redux三分钟入门
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Redux三分钟入门"><a href="#Redux三分钟入门" class="headerlink" title="Redux三分钟入门"></a>Redux三分钟入门</h1><h2 id="什么是Redux？"><a href="#什么是Redux？" class="headerlink" title="什么是Redux？"></a>什么是Redux？</h2><p>对于前端页面，从数据层面来说，无非就是一系列的状态组合。<br>在传统的前端开发中，尤其是jQuery横行的年代，状态管理相关的理念一直没有什么起色。<br>自Flux横空出世以来，各种解决方案层出不穷，百花齐放，Redux也是在这种环境下诞生的。<br>如何描述Redux呢？<br>简而言之，它就是一个状态容器，里面存储了整个应用的所有状态。</p>
<blockquote>
<p>Redux的核心思想就是要提供<strong>_可预测</strong>_的状态管理，这对日益膨胀的大型应用来说尤其重要。</p>
</blockquote>
<p>原谅我有一点标题党，因为对于从来没有听说过Redux的人来说，三分钟入门可能真的不够。</p>
<h2 id="预备知识"><a href="#预备知识" class="headerlink" title="预备知识"></a>预备知识</h2><p>Redux中有三个基本概念非常重要：store / reducer / action。<br>Store顾名思义就是状态容器，Redux使用<strong>_createStore</strong>_这个API来创建一个全局的状态容器。这里有一点比较重要的就是：</p>
<blockquote>
<p>Redux应用只能有一个单一的Store</p>
</blockquote>
<p>我们暂时不用深究Redux为何如此设计。<br>Reducer是一个纯函数，它的职责就是用来更新状态容器中的状态，<strong>_这也是Redux中更新状态的唯一途径</strong>_。<br>Action是一个普通JavaScript对象，它是把数据从应用传递到状态容器的载体。<br>将action传递到store中很简单，使用store.dispatch(action)就可以了。</p>
<blockquote>
<p>为了区分不同的数据来源，我们一般约定每个action都必须要有一个type字段。</p>
</blockquote>
<p>这样我们的reducer函数就能根据这个type字段来决定如何修改状态容器。<br>掌握了上面的一些基本概念以后，我们就可以进入正题了，先上一段代码：</p>
<h2 id="DEMO"><a href="#DEMO" class="headerlink" title="DEMO"></a>DEMO</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Redux的两个核心API</span><br><span class="line"> * createStore用于创建状态容器</span><br><span class="line"> * combineReducers用于将多个reducers合并成一个Reducer</span><br><span class="line"> */</span><br><span class="line">let &#123;createStore, combineReducers&#125; = Redux</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 状态容器的初始状态</span><br><span class="line"> * 一般用于同构应用，服务器端返回相关数据</span><br><span class="line"> */</span><br><span class="line">let INITIAL_STATE = &#123;</span><br><span class="line">  counter: &#123;</span><br><span class="line">    count: 0</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * reducer函数，用于变更状态容器中的状态</span><br><span class="line"> * 如果action未知，则原样返回</span><br><span class="line"> * 永远不要修改state，返回一个全新的state</span><br><span class="line"> */</span><br><span class="line">function counter(state = INITIAL_STATE.counter, action) &#123;</span><br><span class="line">  switch (action.type) &#123;</span><br><span class="line">    case &apos;INCREMENT&apos;:</span><br><span class="line">      return &#123;count: state.count + 1&#125;</span><br><span class="line">    case &apos;DECREMENT&apos;:</span><br><span class="line">      return &#123;count: state.count - 1&#125;</span><br><span class="line">    default:</span><br><span class="line">      return state</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 状态容器三个核心方法：</span><br><span class="line"> * subscribe用于监听事件，每当dispatch的时候会执行</span><br><span class="line"> * dispatch用于分发action，这是改变状态容器中state的唯一途径</span><br><span class="line"> * getState获取当前state</span><br><span class="line"> */</span><br><span class="line">let store = createStore(combineReducers(&#123;counter&#125;), INITIAL_STATE)</span><br><span class="line"></span><br><span class="line">store.subscribe(() =&gt;</span><br><span class="line">  console.log(&apos;获取当前状态容器：&apos;, store.getState())</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">store.dispatch(&#123; type: &apos;INCREMENT&apos; &#125;) // count = 1</span><br><span class="line">store.dispatch(&#123; type: &apos;DECREMENT&apos; &#125;) // count = 0</span><br></pre></td></tr></table></figure>
<p><a href="http://codepen.io/simongfxu/pen/eJLwMb?editors=0012" target="_blank" rel="noopener">在线查看DEMO</a><br>上面的DEMO代码逻辑大致如下：</p>
<ol>
<li>先定义reducer</li>
<li>调用createStore创建store</li>
<li>最后dispatch相关的action</li>
</ol>
<p>对应到具体业务中的过程可能是这样：</p>
<ol>
<li>用户点击某个按钮</li>
<li>然后状态容器dispatch这个action</li>
<li>action最后传达到reducer中</li>
<li>reducer根据action的内容来修改状态</li>
<li>状态容器状态发生改变后触发视图更新</li>
</ol>
<p>理解了上面的DEMO代码之后，Redux基本上就算入门啦！</p>
<h2 id="后续深入"><a href="#后续深入" class="headerlink" title="后续深入"></a>后续深入</h2><p>学习完上面的DEMO后，推荐你先看下Redux的一些<a href="http://camsong.github.io/redux-in-chinese/docs/introduction/ThreePrinciples.html" target="_blank" rel="noopener">基本理念</a>，了解Redux背后设计的前因后果，然后再针对性地围绕<a href="http://camsong.github.io/redux-in-chinese/index.html" target="_blank" rel="noopener">Redux官网中文版</a>学习（个人不太推荐从头到尾的精读，时间充裕的话请自便）。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2016/02/21/redux/" data-id="cjf865ufx001c8wp19zx7tjgc" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Redux/">Redux</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/03/12/better-react-2/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Better React 后续
        
      </div>
    </a>
  
  
    <a href="/2016/02/04/better-react/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Better React</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript-解析器/">JavaScript,解析器</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/">React</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Redux/">Redux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SlimerJS/">SlimerJS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/代码管理/">代码管理</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/持续集成/">持续集成</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/JavaScript/" style="font-size: 20px;">JavaScript</a> <a href="/tags/JavaScript-解析器/" style="font-size: 10px;">JavaScript,解析器</a> <a href="/tags/React/" style="font-size: 16.67px;">React</a> <a href="/tags/Redux/" style="font-size: 10px;">Redux</a> <a href="/tags/SlimerJS/" style="font-size: 10px;">SlimerJS</a> <a href="/tags/代码管理/" style="font-size: 10px;">代码管理</a> <a href="/tags/持续集成/" style="font-size: 13.33px;">持续集成</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">三月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">二月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">十二月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/06/">六月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">七月 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">六月 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/11/">十一月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/10/">十月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/07/">七月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/03/">三月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/02/">二月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2011/10/">十月 2011</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2010/01/">一月 2010</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/1970/01/">一月 1970</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/09/09/space/">空格</a>
          </li>
        
          <li>
            <a href="/2016/07/17/parser/">JavaScript Parser资源总结</a>
          </li>
        
          <li>
            <a href="/2016/06/29/slimerjs/">使用SlimerJS将网页输出为PDF</a>
          </li>
        
          <li>
            <a href="/2016/06/26/code-management/">代码管理</a>
          </li>
        
          <li>
            <a href="/2016/06/18/violet/">violet - 值得一试的写作同步小助手</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 Simon Xu<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>